package com.ve.s2g

class Gate(var count: Int = 0, var name: String = "Nobody", var country: String = "Nowhere") {
    @Synchronized
    fun pass(name: String, address: String) {
        this.count.inc()
        this.name = name
        this.country = address
        check()
    }

    @Synchronized
    override fun toString(): String {
        return "No.$count :$name,$country"
    }

    fun check() {
        if (name.get(0) != country.get(0)) {
            println("BROKEN*************${toString()}()")
        }
    }
}


class UserThread(val gate: Gate, val myname: String, val mycountry: String) : Thread() {

    override fun run() {
        println(myname + "BEGIN")
        while (true) {
            gate.pass(myname, mycountry)
        }
    }
}

fun main(args: Array<String>) {
    println("Testing Gate")
    val gate = Gate()
    UserThread(gate, "Bobe", "Brazil").start()
    UserThread(gate, "Chen", "China").start()
    UserThread(gate, "Alice", "America").start()
}